<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * FormWrapper Class - extension to form helpers
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Html
 * @author		Pete Swan
 */
class Form_wrapper {
	function Form_wrapper(){
		$this->CI = & get_instance();
		$this->CI->load->library('html');
		$this->CI->load->helper('form');
	}

	function form_hidden_wrapper( $name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !isset($params[0]) || !is_array($params[0])) $params[0] = array();
		
		$params[0]['name'] = $name;
		$params[0]['id'] = $name;
		if( !empty($value) ){
			$params[0]['value'] = $value;
		}
		
		if(!isset($params[0]['value'])){
			$params[0]['value'] = null;
		}
		
		return call_user_func_array('form_hidden', array($params[0]['name'], $params[0]['value']));
	}

	function form_upload_wrapper( $name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		
		$params[0]['name'] = $name;
		if( !empty($value)){
			$params[0]['value'] = $value;
		}
		return call_user_func_array('form_upload', $params);
	}
	
	function form_related_item( $name, $value, $params){
		$params = is_array($params)?$params:array();
		
		$ret = '';
		if( isset($params[2]) && isset($params[3])){
			$id = $params[3];
			$item_value = $params[2];
			$tag = 'img';
			$attr = array('src' => '');
			$content = null;
			/*
			 * array( "href" => '', "text" => '', attributes => array())
			 */
			$edit = null;
			$delete = null;
			$prefix = '';
			if( isset($params[1]) && is_array($params[1])){
				$tag = isset($params[1]['tag'])&&!empty($params[1]['tag'])?$params[1]['tag']:$tag;
				/*
				 * $attr should be an associative array containing attributes that should contain the value of this item.
				 * the value of the associative array will provide a possible prefix to the value.
				 * e.g.
				 * 
				 * $params[1]['attr'] = array(
				 *   href => 'path/to/something/'
				 * );
				 * 
				 * would result in an element with the href attribute set to 'path/to/something/item_value'
				 */
				$attr = isset($params[1]['attr'])&&is_array($params[1]['attr'])?$params[1]['attr']:$attr;
				$edit = isset($params[1]['edit'])&&is_array($params[1]['edit'])?$params[1]['edit']:$edit;
				$delete = isset($params[1]['delete'])&&is_array($params[1]['delete'])?$params[1]['delete']:$delete;
				$prefix = isset($params[1]['prefix'])?$params[1]['prefix']:$prefix;
				$content = isset($params[1]['content'])?$params[1]['content']:null;
			}
			
			/* should remove keys that are in $attr */
			$ret = '<div class="form_related_item">' . "<$tag " . _parse_form_attributes($params[0], array());
			foreach($attr as $key => $val){
				$ret .= ' ' . $key . '="' . $val . $item_value . '"';
			}
			if($content){
				if( is_array($content)){
					switch($content['type']){
						case 'string':
							$content = $content['value'];
							break;
						case 'index':
							$index = $content['value']>1&&$content['value']<=3?$content['value']:2;
							$content = $params[$index];
							break;
						default:
							$content = 'Click Here';
					}
				}else{
					$this->CI->load->library('TemplateParser');
					$this->CI->templateparser->setString($content);
					$content = $this->CI->templateparser->evalTemplate(array('id' => $id, 'value' => $item_value));
				}
				$ret .= " >$content</$tag>";
			}else{
				$ret .= ' />';
			}
			if( $edit ){
				if( !isset($edit['attr']) || !is_array($edit['attr'])){
					$edit['attr'] = array();
				}
				if( !isset($edit['attr']['class']) || empty($edit['attr']['class'])){
					$edit['attr']['class'] = 'form_related_item_edit_link';
				}
				$edit['href'] = str_replace('#{id}', $id, $edit['href']);
				$ret .= '<a href="' . $edit['href'] . '" ' . _parse_form_attributes($edit['attr'], array()) . ' >' . $edit['text'] . '</a>';
			}
			if( $delete ){
				if( !isset($delete['attr']) || !is_array($delete['attr'])){
					$delete['attr'] = array();
				}
				if( !isset($delete['attr']['class']) || empty($delete['attr']['class'])){
					$delete['attr']['class'] = 'form_related_item_delete_link';
				}
				$delete['href'] = str_replace('#{id}', $id, $delete['href']);
				$ret .= '<a href="' . $delete['href'] . '" ' . _parse_form_attributes($delete['attr'], array()) . ' >' . $delete['text'] . '</a>';
			}
			$ret .= '</div>' . "\n";
		}else{
			$ret = '<input type="hidden" ' . _parse_form_attributes($params[0], array()) . '" />' . "\n";
		}
		
		return $ret;
	}
	
	function form_image_upload( $name, $value, $params){
		$hidden_field = form_hidden( $name, $value);
		if( $value ){
			return $hidden_field . '<img src="' . $value . '" alt="' . $value . '"' . _parse_form_attributes($params[0], array()) . ' />';
		}else{
			return $hidden_field . Form_wrapper::form_upload_wrapper($name . '_upload', $value, $params);
		}
	}
	
	function form_related_image( $name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		
		$src = null;
		if( isset($params[1]) && !empty($params[1])){
			return '<img ' . _parse_form_attributes($params[0], array()) . 'src="' . $params[1] . '" />' . "\n";
		}
		return '<input type="hidden" ' . _parse_form_attributes($params[0], array()) . '" />' . "\n";
	}
	
	function form_related_video( $name, $value, $params){
		$ci = &get_instance();
		$player = $ci->config->item('flash_video_player_uri');
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		
		if( isset($params[1]) && !empty($params[1])){
			return '<a target="_blank" ' . _parse_form_attributes($params[0], array()) . 'href="' . $player . '?video=' . $params[1] . '">' . $params[1] . "</a>\n";
		}
		return '<input type="hidden" ' . _parse_form_attributes($params[0], array()) . '" />' . "\n";
	}
	
	function form_related_image_deletable( $name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		$params[0]['name'] = $name;
		
		$src = null;
		if( isset($params[1]) && !empty($params[1])){
			$delete_name = 'delete_' . $name;
			return '<img ' . _parse_form_attributes($params[0], array()) . 'src="' . $params[1] . '" />' . "\n" . '<br /><input type="checkbox" class="single_image_delete" name="' . $delete_name . '" id="' . $delete_name . '" /><label for="' . $delete_name . '">Delete Image</label>' . "\n";
		}
		return '<input type="hidden" ' . _parse_form_attributes($params[0], array()) . '" />' . "\n";
	}
	
	function form_related_document( $name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		
		if( isset($params[1]) && !empty($params[1])){
			return '<a target="_blank" ' . _parse_form_attributes($params[0], array()) . 'href="' . $params[1] . '">' . $params[1] . "</a>\n";
		}
		return '<input type="hidden" ' . _parse_form_attributes($params[0], array()) . '" />' . "\n";
	}
	
	function form_related_document_editable( $name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		
		if( isset($params[1]) && !empty($params[1])){
			$edit = '';
			if( isset($params[2]) && !empty($params[2])){
				$edit = '<a class="document_edit_link edit_ajax_link" href="' . base_url() . CMF_PREFIX . 'documents/editajax/' . $params[2] . '">edit document</a>';
			}
			return '<a target="_blank" ' . _parse_form_attributes($params[0], array()) . 'href="' . $params[1] . '">' . $params[1] . "</a>$edit\n";
		}
		return '<input type="hidden" ' . _parse_form_attributes($params[0], array()) . '" />' . "\n";
	}

	function form_input_wrapper($name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		
		$params[0]['name'] = $name;
		if( !empty($value) ){
			$params[0]['value'] = $value;
		}
		
		return call_user_func_array('form_input', $params);
	}
	
	function form_checkbox_wrapper($name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		
		$params[0]['name'] = $name;
		if( $value !== null){
			$params[0]['checked'] = $value;	
		}
		$params[0]['value'] = 1;
		
		return call_user_func_array('form_checkbox', $params);
	}
	
	function form_textarea_wrapper($name, $value, $params){
		$params = is_array($params)?$params:array();
		if( !is_array($params[0])) $params[0] = array();
		
		$params[0]['name'] = $name;
		$params[0]['value'] = $value;
		
		return call_user_func_array('form_textarea', $params);
	}
	
	function radio_group_wrapper($name, $value, $params = array()){
		$ret = '';
		if( is_array($params[0])){
			$data = $params[0];
			$data['name'] = $name;
		}else{
			$data = array('name' => $name);
		}
		if( isset($params[1]) && is_array($params[1])){
			$options = $params[1];
		}else{
			$options = array();
		}
		if( isset($params[2]) && is_array($params[2])){
			$selected = $params[2];
		}else{
			$selected = array();
		}
		$none = null;
		if( isset($params[3]) && is_array($params[3]) ){
			$v = isset($params[3]['value'])?$params[3]['value']:'';
			$s = isset($params[3]['text'])?$params[3]['text']:'None';
			$none = '	<option value="' . $v . '"' . ' selected="selected" ' . '>' . $s . '</option>' . "\n";
		}
		/* if there is nothing selected or it's a multiple select add the current value to selected 
		 * probably want to change this and set a 'use existing value' flag
		 */
		if( count($selected) == 0 ){
			$selected[] = $value;
		}
		
		$ret = '<div class="radio_group">' . "\n";
		$ret .= $none;
		foreach( $options as $k => $v){
			$s = in_array($k, $selected);
			$ret .= '	<input type="radio" name="' . $data['name'] . '" value="' . $k . '"' . ($s?' checked="checked" ':'') . ' />' . $v . "\n";
		}
		$ret .= '</div>';
		return $ret;
	}
	
	function form_dropdown_wrapper($name, $value, $params = array()){
		$ret = '';
		if( is_array($params[0])){
			$data = $params[0];
			$data['name'] = $name;
		}else{
			$data = array('name' => $name);
		}
		if(isset($data['multiple'])){
			$data['name'] .= '[]';
		}
		
		if( isset($params[1]) && is_array($params[1])){
			$options = $params[1];
		}else{
			$options = array();
		}
		if( isset($params[2]) && is_array($params[2])){
			$selected = $params[2];
		}else{
			$selected = array();
		}
		$none = null;
		if( isset($params[3]) && is_array($params[3]) ){
			$v = isset($params[3]['value'])?$params[3]['value']:'';
			$s = isset($params[3]['text'])?$params[3]['text']:'None';
			$none = '	<option value="' . $v . '"' . ' selected="selected" ' . '>' . $s . '</option>' . "\n";
		}
		/* if there is nothing selected or it's a multiple select add the current value to selected 
		 * probably want to change this and set a 'use existing value' flag
		 */
		if( count($selected) == 0 || isset($data['multiple'])){
			$selected[] = $value;
		}
		
		$ret = '<select ' . _parse_form_attributes($data, array()) . '>' . "\n";
		$ret .= $none;
		foreach( $options as $k => $v){
			$s = in_array($k, $selected);
			$ret .= '	<option value="' . $k . '"' . ($s?' selected="selected" ':'') . '>' . $v . '</option>' . "\n";
		}
		$ret .= '</select>';
		return $ret;
	}
}
